<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域-->
      <div class="img_wrap">
        <img src="../assets/logo.png" alt="登陆头像" />
      </div>
      <!--登陆表单区域-->
      <el-form
        class="form_wrap"
        label-width="80px"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <el-form-item label="用户名：" prop="username">
          <el-input
            suffix-icon="iconfont icon-account"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>

        <el-form-item label="密码：" prop="password">
          <el-input
            suffix-icon="iconfont icon-password"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item class="btns_wrap">
          <el-button type="primary" class="btn" @click="handleLogin()"
            >登陆</el-button
          >
          <el-button type="info" class="btn" @click="handleResetForm()"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登陆表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 登陆表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: "输入用户名不能为空", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名长度应为3到10个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "输入密码不能为空", trigger: "blur" },
          {
            min: 3,
            max: 6,
            message: "密码长度应为3到6个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    handleResetForm() {
      // console.log(this);
      this.$refs.loginFormRef.resetFields();
      window.sessionStorage.clear();
    },
    handleLogin() {
      this.$refs.loginFormRef.validate(async (valid) => {
        // valid为true，验证通过，可以用axios发请求，否则不能发请求
        // console.log(valid);
        if (!valid) {
          // 前端验证没通过
          return;
        } else {
          const { data: dataReNameRes } = await this.$http.post(
            "login",
            this.loginForm
          );
          // console.log(dataReNameRes);
          if (dataReNameRes.meta.status !== 200) {
            // 前端校验通过，成功发起请求，但后台验证不通过
            return this.$message.error("用户名或密码错误！");
          }
          this.$message.success("登陆成功！");
          // console.log(dataReNameRes);
          // 登陆成功后将获取的token存到sessionStorage中
          window.sessionStorage.setItem("token", dataReNameRes.data.token);
          // 通过编程式导航跳转到后台主页
          // console.log(this);
          this.$router.push("/home");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 480px;
  height: 320px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;

  .img_wrap {
    width: 100px;
    height: 100px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }

  .form_wrap {
    bottom: 0;
    position: absolute;
    width: 100%;
    padding: 30px 30px 30px 10px;
    box-sizing: border-box;
  }

  .btns_wrap {
    display: flex;
    justify-content: flex-end;
  }
}
</style>